package org.ielse.frame.view;

import org.ielse.frame.App;
import org.ielse.frame.R;
import org.ielse.frame.t.volley.ImageLoader;
import org.ielse.frame.t.volley.ImageLoader.ImageContainer;
import org.ielse.frame.t.volley.ImageLoader.ImageListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class ImageView extends android.widget.ImageView {

	public static final int TYPE_NORMAL = 0;
	public static final int TYPE_ROUND = 1;
	public static final int TYPE_OVAL = 2;
	protected int type = TYPE_NORMAL;

	protected ImageLoader imageLoader;
	protected ImageListener imageListener;

	protected OnClickListener formalListener;

	public ImageView(Context context) {
		this(context, null);
	}

	public ImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		imageLoader = App.i().getImageLoader();
		imageListener = ImageLoader.getImageListener(this, R.drawable.frame_image_default, R.drawable.frame_image_loading,
				R.drawable.frame_image_fail);
	}

	@Override public void setOnClickListener(OnClickListener l) {
		super.setOnClickListener(l);
		this.formalListener = l;
	}

	public ImageContainer loadImage(final String url) {
		ImageContainer imageContainer = imageLoader.get(url, imageListener);

		if (imageContainer.getBitmap() == null && App.i().getAppSettings().isDownloadImageJustWifi()) {
			setOnClickListener(new OnClickListener() {
				@Override public void onClick(View arg0) {
					imageLoader.get(url, imageListener, false);
				}
			});
		}

		return imageContainer;
	}

	@Override public void setImageResource(int resId) {
		super.setImageResource(resId);
		//
		super.setOnClickListener(formalListener);
	}

	@Override public void setImageBitmap(Bitmap bm) {
		switch (type) {
		case TYPE_ROUND:
			bm = round(bm, bm.getWidth() / 10);
			break;
		case TYPE_OVAL:
			bm = oval(bm);
			break;
		}
		super.setImageBitmap(bm);

	}

	protected static Bitmap round(Bitmap resource, float roundSize) {
		Bitmap output = Bitmap.createBitmap(resource.getWidth(), resource.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		final RectF rectF = new RectF(0, 0, resource.getWidth(), resource.getHeight());
		canvas.drawRoundRect(rectF, roundSize, roundSize, paint);
		paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(resource, 0, 0, paint);
		return output;
	}

	protected static Bitmap oval(Bitmap resource) {
		Bitmap output = Bitmap.createBitmap(resource.getWidth(), resource.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		final RectF rectF = new RectF(0, 0, resource.getWidth(), resource.getHeight());
		canvas.drawOval(rectF, paint);
		paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(resource, 0, 0, paint);
		return output;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
}
